package com.venus.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.venus.common.Commons;
import com.venus.entity.Page;
import com.venus.entity.User;
import com.venus.service.BaseService;
import com.venus.service.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by admin on 27/6/17.
 */
@Controller
@RequestMapping("/base")
public class BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);

	@Resource
	private BaseService baseService;

	@Resource MenuService menuService;

	@Resource
	private RedisTemplate<String,Object> redisTemplate;

	/**
	 * 登录页面
	 * @return
	 */
	@RequestMapping("login")
	public String login() {
		return "base/login";
	}

	/**
	 * 注册页面
	 * @return
	 */
	@RequestMapping("register")
	public String register() {
		return "base/register";
	}

	/**
	 * 没有权限页面
	 * @return
	 */
	@RequestMapping("noPermission")
	public String noPermission() {
		return "base/noPermission";
	}

	/**
	 * 登录提交、权限验证
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("loginForm")
	@ResponseBody
	public Map<String,String> loginForm(User user, HttpServletRequest request) {
		Map<String,String> loginUser = baseService.loginValidate(user);

		//登录成功，放入session中
		if("success".equals(loginUser.get("result"))) {
			request.getSession().setAttribute(Commons.SESSION_USER_KEY,user.getUsername());
		}
		LOGGER.info("=============================================================");
		LOGGER.info("用户:" + user.getUsername() + "登录，时间 :" + new Date());
		LOGGER.info("=============================================================");
		return loginUser;
	}

	/**
	 * 新用户注册
	 * 学生完成
	 * @param params
	 * @return
	 */
	@RequestMapping("registerForm")
	@ResponseBody
	public Map<String,String> registerForm(@RequestParam() Map<String,Object> params) {
		Map<String,String> result = new HashMap<>();
		LOGGER.info(params.toString());
		result.put("result","success");
		return result;
	}

	/**
	 * 首页
	 * @return
	 */
	@RequestMapping("home")
	public ModelAndView home(String loginUser) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("base/home");
		List<LinkedHashMap<String,String>> list = menuService.findAllMenus();
		String result = JSONArray.toJSONString(list);
		modelAndView.addObject("result",result);
		modelAndView.addObject("loginUser",null == loginUser ? "未登录" : loginUser);
		return modelAndView;
	}

	/**
	 * 分页例子
	 * @return
	 */
	@RequestMapping("mainPage")
	public ModelAndView mainPage(User user,Page page){
		ModelAndView modelAndView = new ModelAndView();
		page.setObject(user);
		PageInfo<User> pageInfo = baseService.findUsersByPageList(page);
		modelAndView.addObject("page",pageInfo);
		modelAndView.addObject("params",user);
		modelAndView.addObject("list",pageInfo.getList());
		modelAndView.setViewName("base/mainPage");
		return modelAndView;
	}
}
